package com.itheima.controller;

import com.itheima.annotation.LogAnnotation;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Emp;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/depts")

public class DeptController {

    @Autowired
    private DeptService deptService;


    // 查询所有的部门列表
    @LogAnnotation
    @GetMapping

    public Result getDeptList() {
        // Object userInfo = httpSession.getAttribute("userInfo");
        // System.out.println("userInfo = " + userInfo);
        //
        // if(userInfo == null){
        //     return Result.error("请重新登录");
        // }

        List<Dept> list = deptService.getDeptList();
        return Result.success(list);
    }
    // @GetMapping
    // public Result getDeptList(HttpSession httpSession) {
    //     Object userInfo = httpSession.getAttribute("userInfo");
    //     System.out.println("userInfo = " + userInfo);
    //
    //     if(userInfo == null){
    //         return Result.error("请重新登录");
    //     }
    //
    //     List<Dept> list = deptService.getDeptList();
    //     return Result.success(list);
    // }

//     根据id 删除指定部门 /depts?id=1

    @DeleteMapping
    // @PathVariable
    public Result delDeptById(@RequestParam("id") Integer id) throws Exception {

        deptService.delDeptById(id);
        return Result.success();
    }

//     新增1个 部门

    @PostMapping
    public Result insertDept(@RequestBody Dept dept){


        System.out.println(dept);
        deptService.insertDept(dept);
        return Result.success();
    }


    // 测试 @RequestParam 注解用的
    @GetMapping("/test")
    public Result  test(@RequestParam(defaultValue = "1",required = false,name="id") Integer deptId){

        System.out.println("deptId = " + deptId);

        return Result.success();

    }


//     修改
    @GetMapping("/{id}")
    public Result getEmpById(@PathVariable Integer id){

      Dept dept = deptService.getEmpById(id);

        System.out.println("dept = " + dept);

        return Result.success(dept);
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept){

        deptService.updateDept(dept);

        return Result.success();
    }


}
